package com.example.demo.durable;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@RabbitListener(queues = DurableQueueConfig.ACK_QUEUE)
public class DurableAckReciver {

    @RabbitHandler
    public void handler(String msg, Channel channel, Message message) {

        System.out.println("收到消息：" + msg);
        try {
            //消息的标识，false只确认当前一个消息收到，true确认所有consumer获得的消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

            //ack返回false，并重新回到队列
//            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);

            //拒绝消息
//            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
